package tech.aistar.controller.dynamic;


import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.User;
import tech.aistar.entity.UserInfo;
import tech.aistar.entity.UserLogin;
import tech.aistar.entity.dynamic.ImgsStorage;
import tech.aistar.entity.dynamic.Picture;
import tech.aistar.entity.dynamic.PrisePic;
import tech.aistar.service.*;
import tech.aistar.vo.PictureVo;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * Created by David on 2019/10/9.
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/pic")
public class PictureController {
    @Autowired
    private IPictureService pictureService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserLoginService userLoginService;
    @Autowired
    private IPictureVoService pictureVoService;
    @Autowired
    private IPrisePicService prisePicService;
//    @RequestMapping("/picture")
//    public String picture(){
//        return "/dynamic/picture";
//    }

    @RequestMapping("/list")
    public String list(){
//        // 数据的回显
//        model.addAttribute("picId",picId);
//
//        //调用service层方法
//        List<Picture> picList=pictureService.findAll();

//        for (Picture p:picList) {
//            UserInfo userInfo=userInfoService.findById(p.getUserId());
//            model.addAttribute("userInfo"+p.getUserId(),userInfo);
//        }
//        for (int i=0;i<picList.size();i++){
//            Integer userId=picList.get(i).getUserId();
//            UserInfo userInfo=userInfoService.findById(userId);
//            picList.get(i).setUserInfo(userInfo);
//        }
//        model.addAttribute("picList",picList);
        //调用service - 分页
//        PageInfo<Picture> pageInfo = pictureService.findAll2(picId,null,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
//        model.addAttribute("pageInfo",pageInfo);

//        List<ImgsStorage> img=pictureService.findImg();
//        model.addAttribute("img",img);
        return "/dynamic/picture";
    }

    @RequestMapping("/list1")
    @ResponseBody
    public List<PictureVo> list1(Model model, HttpServletRequest req){
        System.out.println("========================");
        //调用service层方法
        UserLogin userLogin= (UserLogin) req.getSession().getAttribute("user");
        List<PictureVo> picList1=pictureVoService.findAll(userLogin==null?null:userLogin.getId());

        for (int i=0;i<picList1.size();i++){
            Integer userId=picList1.get(i).getPicture().getUserId();
            UserInfo userInfo=userInfoService.findById(userId);
            picList1.get(i).getPicture().setUserInfo(userInfo);
        }
        model.addAttribute("picList",picList1);

        return picList1;
    }
    @RequestMapping("/prise")
    @ResponseBody
    public String prise(HttpServletRequest req,Integer picId){
        UserLogin userLogin= (UserLogin) req.getSession().getAttribute("user");
        if(userLogin==null){
            return "未登入";
        }
        PrisePic prisePic=prisePicService.findByPidByUid(picId,userLogin.getId());
        if(prisePic==null){
            PrisePic prisePic1=new PrisePic();
            prisePic1.setUserId(userLogin.getId());
            prisePic1.setPicId(picId);
            prisePicService.insertAll(prisePic1);
            return "ok";
        }else {
            prisePicService.delByPidByUid(picId,userLogin.getId());
            return "已点赞";
        }
    }
}
